<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-form-item label="规则名称" prop="name">
        <el-input v-model="formItem.name" placeholder="请输入规则日名称" clearable />
      </el-form-item>
      <el-form-item label="班次" prop="classesId">
        <el-select v-model="formItem.classesId" clearable filterable remote placeholder="请选择班次">
          <el-option v-for="(item) in classesList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="小组数" prop="groupNumber">
        <el-input-number v-model="formItem.groupNumber" :min="1" :max="20" :precision="0" label="小组数" style="width:100%" />
      </el-form-item>
      <el-form-item label="包含节假日" prop="isIncludedHoliday">
        <el-select v-model="formItem.isIncludedHoliday" filterable remote placeholder="请选择是否包含节假日">
          <el-option v-for="(item) in isIncludedHolidayList" :key="item.code" :label="item.name" :value="item.code" />
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  create,
  update,
  detail
} from '@neu/dcom-ui/api/schedulingrule'
import { list } from '@neu/dcom-ui/api/classes'

// 定义一个表单原始项
const formItem = {
  name: '',
  classesId: null,
  groupNumber: null,
  isIncludedHoliday: false
}
export default {
  name: 'SchedulingruleEditForm',
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 是否包含节假日列表
      isIncludedHolidayList: [
        { name: '是', code: true },
        { name: '否', code: false }
      ],
      // 班次列表
      classesList: [],
      // 表单验证规则
      rules: {
        isIncludedHoliday: [{
          required: true,
          message: '包含节假日不能为空',
          trigger: 'blur'
        }],
        groupNumber: [{
          required: true,
          message: '小组数不能为空',
          trigger: 'blur'
        }],
        classesId: [{
          required: true,
          message: '班次不能为空',
          trigger: 'blur'
        }],
        name: [{
          required: true,
          message: '规则名称不能为空',
          trigger: 'blur'
        },
        {
          validator: function(rule, value, callback) {
            if (value.length > 100) {
              callback(new Error('最多可输入100个字符'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }]
      }
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: function() {
      this.fetch()
    }
  },
  created() {
    list({}).then(res => { this.classesList = res.data })
    this.fetch()
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      const updated = this.id !== -1
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            if (updated) {
              resolve(update(this.id, this.formItem))
            } else {
              resolve(create(this.formItem))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
    }
  }
}
</script>
